import 'package:chewie/chewie.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:qgchat/bloc/blocProvider.dart';
import 'package:qgchat/bloc/shortvideo/innerBloc.dart';
import 'package:qgchat/common/class/videoCall/index.dart';
import 'package:qgchat/common/global/global.dart';
import 'package:qgchat/http/io.dart';
import 'package:qgchat/http/models/anchorModel.dart';
import 'package:qgchat/http/models/innerpageModel.dart';
import 'package:qgchat/page/live/videoPage/giftBox.dart';
import 'package:qgchat/states/states_model/index.dart';
import 'package:qgchat/widgets/tipsDialog.dart';
// 视频播放

class Innerpage extends StatefulWidget {
  Innerpage(this.thumall, {Key key}) : super(key: key);
  final String thumall;
  @override
  _InnerpageState createState() => _InnerpageState();
}

class _InnerpageState extends State<Innerpage> {
  // 视频详情bloc
  InnerBloc bloc;
// Lifecycle

  @override
  void initState() {
    super.initState();
  }

  // 销毁
  @override
  void dispose() {
    super.dispose();
  }

// -------------------------------------------------------------

// methods
// 举报小视频
  reportVideo(videoid, id) {
    Io.get('vedio/Complaintvedio', data: {'vedio_id': videoid, 'number': id})
        .then((res) {
      print(res);
      if (res['status'] == 1) {
        showToast("举报成功，官方会马上核实");
      } else {
        showToast(res['msg']);
      }
    });
  }

// ----------------------------------------------------

  // 后退和喜欢按钮
  Widget backandfocus(type) {
    InnerBloc bloc = BlocProvider.of<InnerBloc>(context);
    return Container(
      child: Column(
        children: <Widget>[
          StreamBuilder(
            stream: bloc.cloveStream,
            builder: (BuildContext context, AsyncSnapshot snapshot) {
              print('------------');
              print(snapshot.data);
              print('------------');
              if (snapshot.hasData) {
                if (snapshot.data == 0) {
                  if (type == 0) {
                    return GestureDetector(
                      behavior: HitTestBehavior.translucent,
                      onTapUp: (val) {
                        bloc.lovecurrent();
                      },
                      child: Container(
                        margin: EdgeInsets.only(
                          bottom: px(30),
                        ),
                        child: Image.asset(
                          'assets/images/icon/focus.png',
                          width: px(58),
                          height: px(53),
                        ),
                      ),
                    );
                  } else {
                    return Container();
                  }
                } else {
                  return GestureDetector(
                    behavior: HitTestBehavior.translucent,
                    onTapUp: (val) {
                      bloc.cancelcurrent();
                    },
                    child: Container(
                      margin: EdgeInsets.only(
                        bottom: px(30),
                      ),
                      child: Image.asset(
                        'assets/images/icon/selectlove.png',
                        width: px(58),
                        height: px(53),
                      ),
                    ),
                  );
                }
              } else {
                return Container();
              }
            },
          ),
          Container(
            child: GestureDetector(
              behavior: HitTestBehavior.translucent,
              onTapUp: (val) {
                Navigator.of(context).pop();
              },
              child: Container(
                width: px(70),
                height: px(70),
                decoration: BoxDecoration(
                  color: Color.fromRGBO(4, 4, 15, 0.4),
                  borderRadius: BorderRadius.all(
                    Radius.circular(50),
                  ),
                ),
                child: Icon(
                  Icons.clear,
                  color: Colors.white,
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  // 礼物预约转发
  Widget features(title, price, token, id) {
    return Container(
      width: px(350),
      child: Column(
        children: <Widget>[
          Container(
            width: px(300),
            margin: EdgeInsets.only(
              bottom: px(49),
            ),
            child: Text(
              title,
              textAlign: TextAlign.left,
              style: TextStyle(
                fontSize: sp(24),
                color: Colors.white,
                decoration: TextDecoration.none,
              ),
            ),
          ),
          Row(
            children: <Widget>[
              GestureDetector(
                onTapUp: (val) {
                  showModalBottomSheet(
                    context: context,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.only(
                        topLeft: Radius.circular(px(24)),
                        topRight: Radius.circular(px(24)),
                      ),
                    ),
                    builder: (BuildContext context) {
                      return GiftBox(
                        token: token,
                        vid: id,
                      );
                    },
                  );
                },
                child: Image.asset(
                  'assets/images/icon/gift_org.png',
                  width: px(66),
                  height: px(66),
                ),
              ),
              GestureDetector(
                onTapUp: (val) => TipsDialog.tryVideo(context, price, () {
                  VideoCall.checkUserStatusAndGoVideoPage(context, token);
                }),
                child: Container(
                  margin: EdgeInsets.only(
                    left: px(50),
                  ),
                  child: Image.asset(
                    'assets/images/icon/video_call.png',
                    width: px(70),
                    height: px(70),
                  ),
                ),
              ),
              GestureDetector(
                onTapUp: (val) {
                  showCupertinoModalPopup<int>(
                    context: context,
                    builder: (context) {
                      return CupertinoActionSheet(
                        message: Text(
                          '请选择举报类型',
                          style: TextStyle(
                            color: col("606266"),
                            fontSize: px(26),
                          ),
                        ),
                        cancelButton: CupertinoActionSheetAction(
                          onPressed: () => Navigator.pop(context),
                          child: Text(
                            "取消",
                            style: TextStyle(
                              color: col("157efb"),
                              fontSize: px(40),
                            ),
                          ),
                        ),
                        actions: <Widget>[
                          CupertinoActionSheetAction(
                            onPressed: () {
                              reportVideo(id, 1);
                              Navigator.pop(context, 1);
                            },
                            child: Text(
                              '政治敏感',
                              style: TextStyle(
                                color: col("303133"),
                                fontSize: px(40),
                              ),
                            ),
                          ),
                          CupertinoActionSheetAction(
                            onPressed: () {
                              reportVideo(id, 2);
                              Navigator.pop(context, 2);
                            },
                            child: Text(
                              '色情低俗',
                              style: TextStyle(
                                color: col("303133"),
                                fontSize: px(40),
                              ),
                            ),
                          ),
                          CupertinoActionSheetAction(
                            onPressed: () {
                              reportVideo(id, 3);
                              Navigator.pop(context, 3);
                            },
                            child: Text(
                              '广告骚扰',
                              style: TextStyle(
                                color: col("303133"),
                                fontSize: px(40),
                              ),
                            ),
                          ),
                          CupertinoActionSheetAction(
                            onPressed: () {
                              reportVideo(id, 4);
                              Navigator.pop(context, 3);
                            },
                            child: Text(
                              '人身攻击',
                              style: TextStyle(
                                color: col("303133"),
                                fontSize: px(40),
                              ),
                            ),
                          ),
                        ],
                      );
                    },
                  );
                },
                child: Container(
                  margin: EdgeInsets.only(
                    left: px(50),
                  ),
                  child: Image.asset(
                    'assets/images/icon/report.png',
                    width: px(70),
                    height: px(70),
                  ),
                ),
              ),
            ],
          )
        ],
      ),
    );
  }

  Widget judgeplay(token) {
    InnerBloc bloc = BlocProvider.of<InnerBloc>(context);
    return StreamBuilder(
      stream: bloc.isplayvideoSteam,
      builder: (BuildContext context, AsyncSnapshot snapshot) {
        if (snapshot.hasData) {
          if (snapshot.data) {
            return Positioned(
              child: GestureDetector(
                behavior: HitTestBehavior.translucent,
                onTapUp: (val) {
                  bloc.videoPlayerController1.pause();
                },
                child: Container(
                  width: double.infinity,
                  height: double.infinity,
                ),
              ),
            );
          } else {
            return Positioned(
              child: Center(
                child: GestureDetector(
                  behavior: HitTestBehavior.translucent,
                  onTapUp: (val) {
                    bloc.videoPlayerController1.play();
                    // if (bloc.videoInfo.paymoney > 0 &&
                    //     bloc.videoInfo.paystatus == 0) {
                    //   showDialog(
                    //     context: context,
                    //     builder: (BuildContext context) {
                    //       return CupertinoAlertDialog(
                    //         title: Text("提示"),
                    //         content: SizedBox(
                    //           height: px(56),
                    //           child: SingleChildScrollView(
                    //             child: ListBody(
                    //               children: <Widget>[
                    //                 Text("观看此视频需要${bloc.videoInfo.paymoney}金币"),
                    //                 Text("是否使用${bloc.videoInfo.paymoney}金币观看？"),
                    //               ],
                    //             ),
                    //           ),
                    //         ),
                    //         actions: <Widget>[
                    //           CupertinoDialogAction(
                    //             child: Text("取消"),
                    //             onPressed: () {
                    //               Navigator.pop(context);
                    //             },
                    //           ),
                    //           CupertinoDialogAction(
                    //             child: Text("确定"),
                    //             onPressed: () {
                    //               bloc.buyvideo(token).then((res) {
                    //                 if (res['status'] == 1) {
                    //                   bloc.videoPlayerController1.play();
                    //                   bloc.videoInfo.paystatus = 1;
                    //                 } else {
                    //                   showToast(res['msg']);
                    //                 }
                    //               });
                    //               Navigator.pop(context);
                    //             },
                    //           ),
                    //         ],
                    //       );
                    //     },
                    //   );
                    // } else {
                    //   bloc.videoPlayerController1.play();
                    // }
                  },
                  child: Image.asset(
                    'assets/images/icon/play.png',
                    width: px(80),
                    height: px(80),
                  ),
                ),
              ),
            );
          }
        } else {
          return Container();
        }
      },
    );
  }

  // 主��ID头像
  Widget anchor(image, name, id) {
    InnerBloc bloc = BlocProvider.of<InnerBloc>(context);
    return Container(
      width: px(350),
      decoration: BoxDecoration(
        color: Color.fromRGBO(0, 0, 0, 0.24),
        borderRadius: BorderRadius.all(
          Radius.circular(
            px(30),
          ),
        ),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          Container(
            width: px(70),
            height: px(70),
            decoration: BoxDecoration(
              image: DecorationImage(
                  image: NetworkImage(image), fit: BoxFit.cover),
              borderRadius: BorderRadius.all(
                Radius.circular(50),
              ),
            ),
          ),
          Container(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Text(
                  name,
                  style: TextStyle(
                    decoration: TextDecoration.none,
                    fontSize: sp(26),
                    color: Colors.white,
                  ),
                ),
                Text(
                  'id: $id',
                  style: TextStyle(
                    decoration: TextDecoration.none,
                    fontSize: sp(20),
                    color: Colors.white,
                  ),
                )
              ],
            ),
          ),
          StreamBuilder(
            stream: bloc.focusStream,
            builder: (BuildContext context, AsyncSnapshot snapshot) {
              if (snapshot.hasData) {
                if (snapshot.data == 0) {
                  return Container(
                    width: px(78),
                    height: px(70),
                    child: RaisedButton(
                      padding: EdgeInsets.all(0),
                      onPressed: () => bloc.focusAnchor(),
                      color: col('ac70ff'),
                      textColor: Colors.white,
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(28),
                      ),
                      child: Text('关注'),
                    ),
                  );
                } else {
                  return Container(
                    width: px(78),
                    height: px(70),
                    child: RaisedButton(
                      padding: EdgeInsets.all(0),
                      onPressed: () => bloc.cancelAnchor(),
                      color: col('ac70ff'),
                      textColor: Colors.white,
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(28),
                      ),
                      child: Image.asset(
                        'assets/images/icon/ok.png',
                        width: px(32),
                        height: px(21),
                      ),
                    ),
                  );
                }
              } else {
                return Container();
              }
            },
          ),
        ],
      ),
    );
  }

  // 每周土豪榜
  Widget tyrant(id) {
    List<Closeman> list;
    InnerBloc bloc = BlocProvider.of<InnerBloc>(context);
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTapUp: (val) {
        goPage(context, '/giftranking', param: {'id': id});
      },
      child: StreamBuilder(
        stream: bloc.localtycoonStream,
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          if (snapshot.hasData && snapshot.data.length > 0) {
            if (snapshot.data.length > 4) {
              list = snapshot.data.sublist(0, 3);
            } else {
              list = snapshot.data;
            }
            return Container(
              width: px(250),
              height: px(70),
              child: Row(
                children: list.map<Widget>((f) {
                  return Container(
                    width: px(70),
                    height: px(70),
                    decoration: BoxDecoration(
                      image: DecorationImage(
                          image: NetworkImage(f.memberHead), fit: BoxFit.cover),
                      borderRadius: BorderRadius.all(
                        Radius.circular(50),
                      ),
                    ),
                  );
                }).toList(),
              ),
            );
          } else {
            return Container(
              width: px(250),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  Container(
                    child: Image.asset(
                      'assets/images/icon/nobody.png',
                      width: px(70),
                      height: px(70),
                    ),
                  ),
                  Text(
                    '虚位以待',
                    style: TextStyle(
                      fontSize: sp(28),
                      color: Colors.white,
                      decoration: TextDecoration.none,
                    ),
                  ),
                  Container(
                    child: Icon(
                      Icons.chevron_right,
                      color: Colors.white,
                    ),
                  )
                ],
              ),
            );
          }
        },
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    InnerBloc bloc = BlocProvider.of<InnerBloc>(context);
    return StreamBuilder(
      stream: bloc.videoInfoStream,
      builder: (BuildContext context, AsyncSnapshot snapshot) {
        InnerpageModel list = snapshot.data;
        UserState user = state(context, 'user');
        if (snapshot.hasData) {
          if (user.info.memberType == 1) {
            return Stack(
              alignment: new Alignment(0.6, 0.6),
              children: <Widget>[
                StreamBuilder(
                  stream: bloc.isonloadStream,
                  builder: (BuildContext context, AsyncSnapshot snapshot) {
                    if (snapshot.hasData) {
                      return Container(
                        width: double.infinity,
                        height: double.infinity,
                        child: Chewie(
                          controller: ChewieController(
                            videoPlayerController: bloc.videoPlayerController1,
                            autoInitialize: false,
                            aspectRatio: MediaQuery.of(context).size.width /
                                MediaQuery.of(context).size.height,
                            autoPlay: false,
                            looping: true,
                            showControls: false,
                          ),
                        ),
                      );
                    } else {
                      return Material(
                        type: MaterialType.transparency,
                        child: Container(
                          width: double.infinity,
                          height: double.infinity,
                          decoration: BoxDecoration(
                            image: DecorationImage(
                              image: NetworkImage(widget.thumall),
                              fit: BoxFit.cover,
                            ),
                          ),
                          child: Center(
                            child: Container(
                              width: 200,
                              height: 100,
                              padding: EdgeInsets.all(10),
                              child: Column(
                                children: <Widget>[
                                  CircularProgressIndicator(),
                                  Text(
                                    '视频加载中...',
                                    style: TextStyle(
                                        fontSize: 12, color: Colors.grey),
                                    softWrap: false,
                                  )
                                ],
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceEvenly,
                              ),
                            ),
                          ),
                        ),
                      );
                    }
                  },
                ),
                judgeplay(user.info.memberToken),
                // 后退喜欢
                Positioned(
                  bottom: px(82),
                  right: px(30),
                  child: backandfocus(1),
                ),
                // 主播id和头像
                Positioned(
                  top: px(102),
                  left: px(30),
                  child: anchor(list.memberHead, list.memberNickname, list.uid),
                ),
                // 每周土豪
                Positioned(
                  top: px(102),
                  right: px(50),
                  child: tyrant(list.id),
                ),
              ],
            );
          } else {
            return Stack(
              alignment: new Alignment(0.6, 0.6),
              children: <Widget>[
                StreamBuilder(
                  stream: bloc.isonloadStream,
                  builder: (BuildContext context, AsyncSnapshot snapshot) {
                    if (snapshot.hasData) {
                      return Container(
                        width: double.infinity,
                        height: double.infinity,
                        child: Chewie(
                          controller: ChewieController(
                            videoPlayerController: bloc.videoPlayerController1,
                            autoInitialize: false,
                            aspectRatio: MediaQuery.of(context).size.width /
                                MediaQuery.of(context).size.height,
                            autoPlay: false,
                            looping: true,
                            showControls: false,
                          ),
                        ),
                      );
                    } else {
                      return Material(
                        type: MaterialType.transparency,
                        child: Container(
                          width: double.infinity,
                          height: double.infinity,
                          decoration: BoxDecoration(
                            image: DecorationImage(
                              image: NetworkImage(widget.thumall),
                              fit: BoxFit.cover,
                            ),
                          ),
                          child: Center(
                            child: Container(
                              width: 200,
                              height: 100,
                              padding: EdgeInsets.all(10),
                              child: Column(
                                children: <Widget>[
                                  CircularProgressIndicator(),
                                  Text(
                                    '视频加载中...',
                                    style: TextStyle(
                                        fontSize: 12, color: Colors.grey),
                                    softWrap: false,
                                  )
                                ],
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceEvenly,
                              ),
                            ),
                          ),
                        ),
                      );
                    }
                  },
                ),
                judgeplay(user.info.memberToken),
                // 后退喜欢
                Positioned(
                  bottom: px(82),
                  right: px(30),
                  child: backandfocus(0),
                ),
                // 礼物预约转发
                Positioned(
                  bottom: px(82),
                  left: px(30),
                  child: features(
                      list.title, list.timePrice, list.memberToken, list.id),
                ),
                // 主播id���头像
                Positioned(
                  top: px(102),
                  left: px(30),
                  child: anchor(list.memberHead, list.memberNickname, list.uid),
                ),
                // 每周土豪
                Positioned(
                  top: px(102),
                  right: px(50),
                  child: tyrant(list.id),
                ),
              ],
            );
          }
        } else {
          return Material(
            type: MaterialType.transparency,
            child: Container(
              width: double.infinity,
              height: double.infinity,
              decoration: BoxDecoration(
                image: DecorationImage(
                  image: NetworkImage(widget.thumall),
                  fit: BoxFit.cover,
                ),
              ),
              child: new Center(
                child: new Container(
                  width: 200,
                  height: 100,
                  padding: EdgeInsets.all(10),
                  child: new Column(
                    children: <Widget>[
                      CircularProgressIndicator(),
                      new Text(
                        '视频加载中...',
                        style: TextStyle(fontSize: 12, color: Colors.grey),
                        softWrap: false,
                      )
                    ],
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  ),
                ),
              ),
            ),
          );
        }
      },
    );
  }
}
